package com.atguigu.gmall.product.controller;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.SpuManagerService;
import com.atguigu.gmall.rabbit.constant.RabbitConst;
import com.atguigu.gmall.rabbit.service.RabbitService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Api("商品属性spu管理")
public class SpuManagerController {

    @Autowired
    private SpuManagerService spuManagerSerice;

    @Autowired
    private RabbitService rabbitService;


    @ApiOperation("spu分页列表")
    @GetMapping("{page}/{limit}")
    public Result spuInfoPage(@PathVariable Integer page,
                              @PathVariable Integer limit,
                              @RequestParam("category3Id") Long category3Id ){
        Page<SpuInfo> spuInfoPage =  spuManagerSerice.spuInfoPageList(page,limit,category3Id);
        return Result.ok(spuInfoPage);
    }
    @ApiOperation("品牌列表")
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
       List<BaseTrademark> list =  spuManagerSerice.getTrademarkList();
       return Result.ok(list);
    }

    @ApiOperation("销售属性列表")
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> spuSaleAttrs = spuManagerSerice.baseSaleAttrList();
        return Result.ok(spuSaleAttrs);
    }

    @ApiOperation("添加spu")
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        spuManagerSerice.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
      List<SpuImage> spuImages =   spuManagerSerice.spuImageList(spuId);
      return Result.ok(spuImages);
    }

    //sku分页列表
    @ApiOperation("sku分页列表")
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuList(@PathVariable Integer page,@PathVariable Integer limit){
       Page<SkuInfo> skuInfoPage =  spuManagerSerice.getSkuList(page,limit);
       return Result.ok(skuInfoPage);
    }
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrs = spuManagerSerice.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrs);
    }
    @ApiOperation("添加sku")
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        spuManagerSerice.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    @ApiOperation("商品上架")
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        spuManagerSerice.onSale(skuId);
        rabbitService.sendMessage(RabbitConst.EXCHANGE_DIRECT_GOODS,RabbitConst.ROUTING_GOODS_UPPER,skuId);
        return Result.ok();
    }
    @ApiOperation("商品下架")
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        spuManagerSerice.cancelSale(skuId);
        rabbitService.sendMessage(RabbitConst.EXCHANGE_DIRECT_GOODS,RabbitConst.ROUTING_GOODS_LOWER,skuId);
        return Result.ok();
    }


}
